# -*- coding: utf-8 -*- """ Created on Mon Jul 12 14:46:44 2021 @author: Ji """ import networkx as nx import numpy as np def get_separation_within_set(nodes_from, lengths=None): values = [] # Distance to closest node within the set (A or B) for source_id in nodes_from: inner_values = [] for target_id in nodes_from: if source_id == target_id: continue d = nx.shortest_path_length(G, source_id, target_id) inner_values.append(d) values.append(np.min(inner_values)) return values def get_separation_between_sets(nodes_from, nodes_to): values = [] for source_id in nodes_from: source_to_values = [] for target_id in nodes_to: d = nx.shortest_path_length(G, source_id, target_id) source_to_values.append(d) values.append(np.min(source_to_values)) for target_id in nodes_to: target_to_values = [] for source_id in nodes_from: d = nx.shortest_path_length(G, target_id, source_id) target_to_values.append(d) values.append(np.min(source_to_values)) return values def get_separation(nodes_from, nodes_to): dAA = np.mean(get_separation_within_set(nodes_from)) dBB = np.mean(get_separation_within_set(nodes_to)) dAB = np.mean(get_separation_between_sets(nodes_from, nodes_to)) d = dAB - (dAA + dBB) / 2.0 return d